﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.HomeServer.Controls;
using Microsoft.HomeServer.SDK.Interop.v1;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Umbrella.Whs.SharePoint;
using Umbrella.Whs;

namespace UmbrellaFoo
{
    public class Program : SharePointConnected
    {

        private void DumpTemplates()
        {
            using (var s = new SPSite("http://vmwhs"))
            {
                using (var w = s.OpenWeb())
                {
                    foreach (SPWebTemplate t in s.GetWebTemplates(1033))
                    {
                        Console.WriteLine(t.Name);
                        Console.WriteLine(t.Title);
                    }
                }
            }
        }

        private void DumpUrls()
        {
            var wm = new WebsiteManager();

            Console.WriteLine(wm.WhsDomainName);
            Console.WriteLine(wm.UmbrellaWebsiteUrl);
            Console.WriteLine(wm.UmbrellaDomainUrl);
        }
        
        private void DumpWhsSharePointApp()
        {
            var wm = new WebsiteManager();

            var app = SPWebApplication.Lookup(wm.UmbrellaWebsiteUrl);

            if (app == null)
            {
                Console.WriteLine("not found: " + wm.WhsWebsiteUrl);

                return;
            }

            Console.WriteLine(app.Id);
            Console.WriteLine(app.Status);
        }

        private void DumpCommonSettings()
        {
            var t = typeof(CommonSettings);
            
            var props = t.GetProperties(BindingFlags.Static | BindingFlags.Public);

            foreach (var p in props)
            {
                try
                {
                    Console.WriteLine(
                        p.Name +
                        " = " +
                        Convert.ToString(p.GetValue(null, null))
                        );
                }
                catch
                {
                    // ignore
                }
            }
        }

        private void DumpSharePointUsers()
        {
            var ws = new Website();

            foreach (var u in ws.GetUsers())
            {
                Console.WriteLine(u.Login);
            }
        }

        private void DumpWhsUsersRolling()
        {
            Console.WriteLine("Abort manually!");

            for (; ; )
            {
                try
                {
                    var ws = new WHSInfoClass();

                    ws.Init(Umbrella.Whs.Properties.Constants.ApplicationName);

                    foreach (IUserInfo u in ws.GetUsersInfo())
                    {
                        Console.WriteLine(u.LogOnName);
                    }
                }
                catch (Exception x)
                {
                    Console.WriteLine(x.Message);
                }                

                System.Threading.Thread.Sleep(5000);
            }
        }

        #region main

        static void Main(string[] args)
        {
            try
            {
                var p = new Program();

                p.DumpWhsUsersRolling();

                Console.WriteLine("DONE.");
            }
            catch (Exception x)
            {
                for (var xx = x; xx != null; xx = xx.InnerException)
                {
                    Console.WriteLine("- FAILED -------------------------------------------");
                    Console.WriteLine(xx.Message);
                    Console.WriteLine(xx.StackTrace);
                }
            }

            Console.ReadKey(true);
        }

        #endregion
    }
}
